import GoodsList from '@/views/GoodsList/GoodsList.vue';
import { createRouter,createWebHistory } from 'vue-router'

const router = createRouter({
    history:createWebHistory(process.env.BASE_URL),
    routes:[
        {
            path:'/',
            name:'home',
            component:() => import('@/views/HomeView.vue'),
            meta:{
                title:'慕西商城首页'
            }
        },
        {
            //这个?的意思是，这个参数你可以传你也可以不传
            path:'/goods_List/:keyword/:page/:order?',
            name:'GoodsList',
            component:() => import('@/views/GoodsList/GoodsList.vue'),
            meta:{
                title:'商品列表页'
            }
        },
        {
            path:'/detail/:sku_id',
            name:'Detail',
            component:() => import('@/views/Goods/Detail.vue'),
            meta:{
                title:'商品详情页',
                isAuthRequired:true
            }
        },
        {
            path:'/login',
            name:'Login',
            component:() => import('@/views/Login/Login.vue'),
            meta:{
                title:'欢迎登录'
            }
        },
        {
            path:'/cart/detail',
            name:'Cart',
            component:() => import('@/views/Cart/Cart.vue'),
            meta:{
                title:'购物车',
            }
        },
        {
            path:'/order/:trade_no',
            name:'Order',
            component:() => import('@/views/Order/Order.vue'),
            meta:{
                title:'订单页面',
                isAuthRequired:true
            }
        },
        {
            path:'/profile',
            name:'Profile',
            component:() => import('@/views/Profile/Profile.vue'),
            meta:{
                title:'个人中心',
                isAuthRequired:true
            }
        },
        {
            path:'/Order/Pay',
            name:'OrderPay',
            component:() => import('@/views/Order/OrderPay.vue'),
            meta:{
                title:'收银台',
                isAuthRequired:true
            }
        }
    ]
})


import store from '@/store'

//配置路由守卫，在路由切换之前执行一些操作，to是目标路由对象，from是即将离开的路由对象
router.beforeEach((to,from) => {
    document.title = to.meta.title
    if(to.meta.isAuthRequired === true && store.state.user.isLogin === false) {
        router.push({
            name:'Login'
        })
    }
})


export default router;

